/*
 * InternalFrame.java
 *
 * Created on November 14, 2008, 2:41 PM
 */

package ca.jerg.canvas;

import ca.jerg.app.*;
import ca.jerg.data.*;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import java.beans.PropertyVetoException;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.event.InternalFrameEvent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameListener;
import javax.swing.undo.UndoableEdit;
import javax.swing.undo.UndoableEditSupport;


/**
 *
 * @author  dave
 */
public class InternalFrame extends javax.swing.JInternalFrame 
		implements ListenerCursorLocation, IInternalFrame, InternalFrameListener
{

	public static void main( String[] args )
	{
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
				
				InternalFrame t = new InternalFrame();
				t.setVisible(true);
				t.setSize(600, 400);
				
				JDesktopPane dp = new JDesktopPane();
				dp.add(t);				
				
				JFrame jf = new JFrame();
				jf.add(dp);
				jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				jf.setSize(640, 480);
				jf.setVisible(true);
            }
        });
	}

	// <editor-fold defaultstate="collapsed" desc="Constructors">
    /** Creates new form InternalFrame */
	public InternalFrame()
	{
		construct();
	}
	
	public InternalFrame( jErg parent, String title )
	{
		this.parent = parent;		
		
		construct();
		setTitle(title);	
	}
	
	public InternalFrame( jErg parent, String title, int windowIndex )
	{
		this.parent = parent;
		this.windowIndex = windowIndex;		
		
		construct();
		setTitle(title);
	}
	
	public InternalFrame( jErg parent, File file )
	{
		this.parent = parent;
		
		construct();
		openFile(file);
	}
	
    public void construct()
	{	
		undoManager = new UndoManager();
		undoSupport = new UndoableEditSupport();
		
		ec = new Canvas(undoManager,undoSupport);
		ec.addCursorLocationListener(this);
		
		fileDialog = new JFileChooser();
		
		dataHeader = new ErgFileHeader();
		dataHeader.setDescription("Empty");
		
        newAction = new NewAction(this,"New",ErgIcons.GENERAL,"New","New",KeyEvent.VK_N);
		openAction = new OpenAction(this,"Open",ErgIcons.GENERAL,"Open","Open",KeyEvent.VK_O);
		closeAction = new CloseAction(this,"Close",ErgIcons.GENERAL,"Stop","Close",KeyEvent.VK_C);
		saveAction = new SaveAction(this,"Save",ErgIcons.GENERAL,"Save","Save",KeyEvent.VK_S);
		cutAction = new CutAction("Cut",ErgIcons.GENERAL,"Cut","Cut",KeyEvent.VK_T);
		copyAction = new CopyAction("Copy",ErgIcons.GENERAL,"Copy","Copy",KeyEvent.VK_Y);
		pasteAction = new PasteAction("Paste",ErgIcons.GENERAL,"Paste","Paste",KeyEvent.VK_P);
		redoAction = new RedoAction("Redo",ErgIcons.GENERAL,"Redo","Redo",KeyEvent.VK_R);
		undoAction = new UndoAction("Undo",ErgIcons.GENERAL,"Undo","Undo",KeyEvent.VK_U);
		clearAction = new NewAction(this,"Clear",ErgIcons.GENERAL,"Remove","Clear",KeyEvent.VK_L);
		stepAction = new StepAction(this,"Step",null,null,"Step",null);
		rampAction = new RampAction(this,"Ramp",null,null,"Ramp",null);
		zoomAction = new ZoomAction(this,"Zoom",ErgIcons.GENERAL,"Zoom","Zoom",KeyEvent.VK_Z);
		propertiesAction = new PropertiesAction(this,"Properties",ErgIcons.GENERAL,"Edit","Properties",KeyEvent.VK_Z);

		addInternalFrameListener(this);

		initComponents();
    }
	// </editor-fold>
	
	jErg parent;
	int windowIndex;
	
	ErgFileHeader dataHeader;
	
	Canvas ec;
	UndoManager undoManager;
	UndoableEditSupport undoSupport;
	
	JFileChooser fileDialog;
	
	NewAction newAction;
	OpenAction openAction;
	SaveAction saveAction;
	CloseAction closeAction;
	
	UndoAction undoAction;
	RedoAction redoAction;
	CutAction cutAction;
	CopyAction copyAction;
	PasteAction pasteAction;
	
	NewAction clearAction;
	
	StepAction stepAction;
	RampAction rampAction;
	
	ZoomAction zoomAction;
	
	PropertiesAction propertiesAction;
	
	// <editor-fold defaultstate="collapsed" desc="Actions">
	public class NewAction extends AbstractAction
	{
		public NewAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic  )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{
			if ( ec.getData().isEdited() == true )
			{
				
				int ret = JOptionPane.showInternalConfirmDialog(ec, "Abandon changes?",
						"Confirm",JOptionPane.YES_NO_OPTION);
				
				if ( ret == JOptionPane.NO_OPTION )
					return;							
			}
						
			ec.getData().removeAll();
			ec.getData().insert(new ErgPoint(0.0,0));
			ec.getData().setEdited(false);
			
			parent.setTitle("Unsaved-" + String.valueOf(windowIndex));
			ec.repaint(0);
		}
		
		private InternalFrame parent;
	}
	
	public class OpenAction extends AbstractAction
	{
		public OpenAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic  )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{
			if ( ec.getData().isEdited() == true )
			{
				
				int ret = JOptionPane.showInternalConfirmDialog(ec, "Abandon changes?",
						"Confirm",JOptionPane.YES_NO_OPTION);
				
				if ( ret == JOptionPane.NO_OPTION )
					return;							
			}
			
			if ( fileDialog.showOpenDialog(ec) == JFileChooser.APPROVE_OPTION )
			{
				openFile(fileDialog.getSelectedFile());
			}
		}
		
		private InternalFrame parent;
	}

	public class CloseAction extends AbstractAction
	{
		public CloseAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{		
			try
			{
				parent.getMasterFrame().removeInternalFrame(parent);
				setClosed(true);
			}
			catch ( PropertyVetoException pve )
			{				
			}
		}
		
		private InternalFrame parent;
	}
	
	public class SaveAction extends AbstractAction
	{
		public SaveAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic  )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{			

			String name = parent.saveFile();
			if ( name != null )
				parent.setTitle(name);
		}
		
		InternalFrame parent;
	}
	
	public class CutAction extends AbstractAction
	{
		public CutAction( String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
			
			setEnabled(false);
		}
		
		public void actionPerformed( ActionEvent e )
		{
			throw new UnsupportedOperationException("Not supported yet.");
		}		
	}
	
	public class CopyAction extends AbstractAction
	{
		public CopyAction( String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
			
			setEnabled(false);
		}
		
		public void actionPerformed( ActionEvent e )
		{
			throw new UnsupportedOperationException("Not supported yet.");
		}		
	}
	
	public class PasteAction extends AbstractAction
	{
		public PasteAction( String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
			
			setEnabled(false);
		}
		
		public void actionPerformed( ActionEvent e )
		{
			throw new UnsupportedOperationException("Not supported yet.");
		}		
	}
	
	public class UndoAction extends AbstractAction
	{
		public UndoAction( String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{
			try
			{
				undoManager.undo();
			}
			catch ( CannotUndoException undo )
			{
			}
		}		
	}
	
	public class RedoAction extends AbstractAction
	{
		public RedoAction( String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));			
		}
		
		public void actionPerformed( ActionEvent e )
		{
			try
			{
				undoManager.redo();
			}
			catch ( CannotRedoException redo )
			{
			}
		}		
	}
	
	public class StepAction extends AbstractAction
	{
		public StepAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic  )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{
			String s = (String)JOptionPane.showInternalInputDialog(ec, "Enter watts", "Add Step", 
					JOptionPane.PLAIN_MESSAGE, null, null, null);
			
			// null is returned if the dialog was cancelled
			if ( s == null )
				return;

			int endWatts = Integer.parseInt(s);
			ErgData data = ec.getData();
			try
			{
				ErgPoint start = data.getLastPoint();
				ErgDataStep es = new ErgDataStep(start.getTime(),start.getWatts(),endWatts);
				ErgData ed = new ErgData();
				ed.insert(es.getData());

				UndoableEdit edit = new EditAdd(ec,ec.getData(),ed);

				data.insert(es.getData());

				undoSupport.postEdit(edit);

				ec.repaint(0);
			}
			catch ( IndexOutOfBoundsException ex )
			{
				return;
			}			
		}
		
		private InternalFrame parent;
	}
	
	public class RampAction extends AbstractAction
	{
		public RampAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic  )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{
			String time = (String)JOptionPane.showInternalInputDialog(ec, "Enter time", "Add Ramp 1", 
					JOptionPane.PLAIN_MESSAGE, null, null, null);
			
			if ( time == null )
				return;
			
			String watts = (String)JOptionPane.showInternalInputDialog(ec, "Enter watts", "Add Ramp 2", 
					JOptionPane.PLAIN_MESSAGE, null, null, null);
			
			// null is returned if the dialog was cancelled
			if ( watts == null )
				return;

			double endTime = Double.parseDouble(time);
			int endWatts = Integer.parseInt(watts);
			
			ErgData data = ec.getData();
			try
			{
				ErgPoint start = data.getLastPoint();
				ErgDataRamp es = new ErgDataRamp(start.getTime(),start.getWatts(),endTime,endWatts);
				ErgData ed = new ErgData();
				ed.insert(es.getData());

				UndoableEdit edit = new EditAdd(ec,ec.getData(),ed);

				data.insert(es.getData());

				undoSupport.postEdit(edit);

				ec.repaint(0);
			}
			catch ( IndexOutOfBoundsException ex )
			{
				return;
			}			
		}
		
		private InternalFrame parent;
	}
	
	public class ZoomAction extends AbstractAction
	{
		public ZoomAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{
			DialogZoom dz = new DialogZoom(ec.getMin(),ec.getMax());
			Object[] options = { "Zoom", "Cancel" };
			int rep = JOptionPane.showInternalOptionDialog(parent,
					dz, "Change Zoom", 
					JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,
					options,options[0]);
			
			if ( rep == JOptionPane.NO_OPTION )
				return;
			
			ErgPoint curMin = ec.getMin();
			ErgPoint curMax = ec.getMax();
			ErgPoint min = new ErgPoint(dz.getMinTime(),dz.getMinWatts());
			ErgPoint max = new ErgPoint(dz.getMaxTime(),dz.getMaxWatts());
			
			UndoableEdit edit = new EditZoom(ec, curMin, curMax, 
					min, max);

			ec.setMax(max);
			ec.setMin(min);
			
			undoSupport.postEdit(edit);
			
			ec.repaint();		
		}
		
		private InternalFrame parent;
	}
	
	public class PropertiesAction extends AbstractAction
	{
		public PropertiesAction( InternalFrame parent,
				String text, String iconType, String iconName, String desc, Integer mnemonic )
		{
			this.parent = parent;
			
			putValue(SHORT_DESCRIPTION, desc);
			putValue(MNEMONIC_KEY, mnemonic);
			putValue(LARGE_ICON_KEY,ErgIcons.createLargeIcon(iconType, iconName));
			putValue(SMALL_ICON,ErgIcons.createSmallIcon(iconType, iconName));
		}
		
		public void actionPerformed( ActionEvent e )
		{
			DialogProperties dp = new DialogProperties((int)ec.getXAxisTickSpace(),(int)ec.getYAxisTickSpace(),
					ec.isShowGrids(),
					(double)ec.getDataLineWidth(),ec.getDataLineColor(),
					ec.getDataPointRadius(),ec.getDataPointColor());

			
			int rep = JOptionPane.showInternalOptionDialog(parent, 
					dp, "Change Propterties",
					JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
					null, null, null);
					
			if ( rep == JOptionPane.CANCEL_OPTION )
				return;
			
			ec.setXAxisTickSpace(dp.getXTick());
			ec.setYAxisTickSpace(dp.getYTick());
			ec.setShowGrids(dp.getGridLines());
			ec.setDataLineColor(dp.getLineColour());
			ec.setDataLineWidth((float)dp.getLineWidth());
			ec.setDataPointColor(dp.getPointColour());
			ec.setDataPointRadius(dp.getPointRadius());
			ec.repaint();
		}
		
		private InternalFrame parent;
	}
	// </editor-fold>

	public void cursorLocation( EventCursorLocation event )
	{
		ErgPoint ep = event.getLocation();
		DecimalFormat df = new DecimalFormat("#.##");
		
		statusBarTime.setText(df.format(ep.getTime()));
		statusBarWatts.setText(String.valueOf(ep.getWatts()));
	}
	
	public void closeWindow()
	{
		if ( ec.getData().isEdited() == true )
		{

			int ret = JOptionPane.showInternalConfirmDialog(ec, "Abandon changes?",
					"Confirm",JOptionPane.YES_NO_OPTION);

			if ( ret == JOptionPane.YES_OPTION )
				return;
			
			saveFile();
		}
	}
	
	public void openFile( File file )
	{
		ErgFile ef = new ErgFile();

		if ( ef.read(file.getAbsolutePath()) != 0 )
		{
			JOptionPane.showInternalMessageDialog(ec,"Unable to open file",
					"Read Error",JOptionPane.ERROR_MESSAGE);

			return;
		}

		ec.setData(ef.getData());

		setTitle(file.getAbsolutePath());
		ec.repaint(0);
	}
	
	public String saveFile()
	{
		if ( fileDialog.showSaveDialog(ec) == JFileChooser.APPROVE_OPTION )
		{
			File file = fileDialog.getSelectedFile();
			ErgFile ef = new ErgFile();
			ef.setData(ec.getData());
			ef.setDescription(dataHeader.getDescription());
			ef.setFileName(file.getName());

			if ( ef.write(file.getAbsolutePath()) == false )
			{
				JOptionPane.showInternalMessageDialog(ec, "Unable to save file",
						"Write Error",JOptionPane.ERROR_MESSAGE);

				return null;
			}
			
			return file.getAbsolutePath();
		}
		
		return null;
	}

	public jErg getMasterFrame()
	{
		return parent;
	}
	
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        canvasPanel = new javax.swing.JPanel();
        statusBar = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        statusBarTime = new javax.swing.JTextField();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jLabel2 = new javax.swing.JLabel();
        statusBarWatts = new javax.swing.JTextField();
        jToolBar1 = new javax.swing.JToolBar();
        newButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        cutButton = new javax.swing.JButton();
        copyButton = new javax.swing.JButton();
        pasteButton = new javax.swing.JButton();
        undoButton = new javax.swing.JButton();
        redoButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        zoomButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JSeparator();
        jMenuItem14 = new javax.swing.JMenuItem();
        insertMenu = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        zoomMenu = new javax.swing.JMenu();
        jMenuItem11 = new javax.swing.JMenuItem();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        canvasPanel.setLayout(new java.awt.BorderLayout());

        canvasPanel.add(ec,java.awt.BorderLayout.CENTER);

        jScrollPane1.setViewportView(canvasPanel);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        statusBar.setFloatable(false);
        statusBar.setRollover(true);

        jLabel1.setLabelFor(statusBarTime);
        jLabel1.setText("Time (m)");
        statusBar.add(jLabel1);

        statusBarTime.setEditable(false);
        statusBarTime.setMargin(new java.awt.Insets(0, 5, 0, 5));
        statusBarTime.setMaximumSize(new java.awt.Dimension(50, 20));
        statusBarTime.setMinimumSize(new java.awt.Dimension(50, 20));
        statusBar.add(statusBarTime);
        statusBar.add(jSeparator5);

        jLabel2.setLabelFor(statusBarWatts);
        jLabel2.setText("Power (W)");
        statusBar.add(jLabel2);

        statusBarWatts.setEditable(false);
        statusBarWatts.setMargin(new java.awt.Insets(0, 5, 0, 5));
        statusBarWatts.setMaximumSize(new java.awt.Dimension(50, 20));
        statusBarWatts.setMinimumSize(new java.awt.Dimension(50, 20));
        statusBar.add(statusBarWatts);

        getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        newButton.setAction(newAction);
        newButton.setFocusable(false);
        newButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(newButton);

        saveButton.setAction(saveAction);
        saveButton.setFocusable(false);
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(saveButton);

        closeButton.setAction(closeAction);
        closeButton.setFocusable(false);
        closeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        closeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(closeButton);
        jToolBar1.add(jSeparator1);

        cutButton.setAction(cutAction);
        cutButton.setFocusable(false);
        cutButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cutButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(cutButton);

        copyButton.setAction(copyAction);
        copyButton.setFocusable(false);
        copyButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        copyButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(copyButton);

        pasteButton.setAction(pasteAction);
        pasteButton.setFocusable(false);
        pasteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        pasteButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(pasteButton);

        undoButton.setAction(undoAction);
        undoButton.setFocusable(false);
        undoButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        undoButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(undoButton);

        redoButton.setAction(redoAction);
        redoButton.setFocusable(false);
        redoButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        redoButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(redoButton);
        jToolBar1.add(jSeparator2);

        zoomButton.setAction(zoomAction);
        zoomButton.setFocusable(false);
        zoomButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        zoomButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(zoomButton);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        fileMenu.setMnemonic('F');
        fileMenu.setText("File");

        jMenuItem12.setAction(newAction);
        jMenuItem12.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem12.setText("New");
        fileMenu.add(jMenuItem12);

        jMenuItem13.setAction(openAction);
        jMenuItem13.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem13.setText("Open");
        fileMenu.add(jMenuItem13);

        jMenuItem1.setAction(saveAction);
        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Save");
        fileMenu.add(jMenuItem1);

        jMenuItem2.setAction(closeAction);
        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Close");
        fileMenu.add(jMenuItem2);

        jMenuBar1.add(fileMenu);

        editMenu.setMnemonic('E');
        editMenu.setText("Edit");

        jMenuItem3.setAction(undoAction);
        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Undo");
        editMenu.add(jMenuItem3);

        jMenuItem4.setAction(redoAction);
        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("Redo");
        editMenu.add(jMenuItem4);
        editMenu.add(jSeparator3);

        jMenuItem5.setAction(cutAction);
        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Cut");
        editMenu.add(jMenuItem5);

        jMenuItem6.setAction(copyAction);
        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Copy");
        editMenu.add(jMenuItem6);

        jMenuItem7.setAction(pasteAction);
        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem7.setText("Paste");
        editMenu.add(jMenuItem7);
        editMenu.add(jSeparator6);

        jMenuItem14.setAction(propertiesAction);
        jMenuItem14.setMnemonic('o');
        jMenuItem14.setText("Properties");
        editMenu.add(jMenuItem14);

        jMenuBar1.add(editMenu);

        insertMenu.setMnemonic('I');
        insertMenu.setText("Insert");

        jMenuItem8.setAction(clearAction);
        jMenuItem8.setMnemonic('C');
        jMenuItem8.setText("Clear");
        insertMenu.add(jMenuItem8);
        insertMenu.add(jSeparator4);

        jMenuItem9.setAction(rampAction);
        jMenuItem9.setMnemonic('R');
        jMenuItem9.setText("Ramp");
        jMenuItem9.setToolTipText("Insert a ramp function at the last  data point");
        insertMenu.add(jMenuItem9);

        jMenuItem10.setAction(stepAction);
        jMenuItem10.setMnemonic('S');
        jMenuItem10.setText("Step");
        jMenuItem10.setToolTipText("Insert a step function at the last  data point");
        insertMenu.add(jMenuItem10);

        jMenuBar1.add(insertMenu);

        zoomMenu.setMnemonic('Z');
        zoomMenu.setText("Zoom");

        jMenuItem11.setAction(zoomAction);
        jMenuItem11.setText("Change Zoom");
        zoomMenu.add(jMenuItem11);

        jMenuBar1.add(zoomMenu);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel canvasPanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton copyButton;
    private javax.swing.JButton cutButton;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu insertMenu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton newButton;
    private javax.swing.JButton pasteButton;
    private javax.swing.JButton redoButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JToolBar statusBar;
    private javax.swing.JTextField statusBarTime;
    private javax.swing.JTextField statusBarWatts;
    private javax.swing.JButton undoButton;
    private javax.swing.JButton zoomButton;
    private javax.swing.JMenu zoomMenu;
    // End of variables declaration//GEN-END:variables

	// <editor-fold defaultstate="collapsed" desc="Internal Frame Listener">
	public void internalFrameClosing( InternalFrameEvent e )
	{
		closeWindow();
		parent.removeInternalFrame(this);
	}
	
	// <editor-fold defaultstate="collapsed" desc="Unused events">
	public void internalFrameOpened( InternalFrameEvent e )
	{
		//throw new UnsupportedOperationException("Not supported yet.");
	}

	public void internalFrameClosed( InternalFrameEvent e )
	{
		//throw new UnsupportedOperationException("Not supported yet.");
	}

	public void internalFrameIconified( InternalFrameEvent e )
	{
		//throw new UnsupportedOperationException("Not supported yet.");
	}

	public void internalFrameDeiconified( InternalFrameEvent e )
	{
		//throw new UnsupportedOperationException("Not supported yet.");
	}

	public void internalFrameActivated( InternalFrameEvent e )
	{
		//throw new UnsupportedOperationException("Not supported yet.");
	}

	public void internalFrameDeactivated( InternalFrameEvent e )
	{
		//throw new UnsupportedOperationException("Not supported yet.");
	}
	// </editor-fold>
	// </editor-fold>
}
